package cn.meibang.common.beauty.provider.service;

import cn.apestar.core.framework.exception.BusinessException;
import cn.apestar.util.BeanUtilEx;
import cn.meibang.common.beauty.api.IBcoUserConsultantLatestAPIService;
import cn.meibang.common.beauty.provider.dao.BeautyConsultationOrderUserConsultantLatestMapper;
import cn.meibang.common.beauty.provider.domain.BeautyConsultationOrderUserConsultantLatest;
import cn.meibang.common.beauty.provider.request.ListBcoUserConsultantLatestRequest;
import cn.meibang.common.beauty.provider.response.BcoUserConsultantLatestResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017-6-24.
 */
@Service
public class BcoUserConsultantLatestServiceProvider implements IBcoUserConsultantLatestAPIService {

    @Autowired
    private BeautyConsultationOrderUserConsultantLatestMapper bcoUserConsultantLatestMapper;

    @Override
    public List<BcoUserConsultantLatestResponse> list(ListBcoUserConsultantLatestRequest request) {
        if (request.getStoreId() == null) {
            throw new BusinessException("order.id.not.found");
        }
        if (request.getUserId() == null && StringUtils.isEmpty(request.getOpenid())) {
            throw new BusinessException("user.not.found");
        }
        List<BeautyConsultationOrderUserConsultantLatest> bcoUserConsultantLatests = bcoUserConsultantLatestMapper
                .selectPageByUserIdAndOpenidAndStoreId(request.getUserId(), request.getOpenid(), request.getStoreId(), request.getOffset(), request.getSize());
        List<BcoUserConsultantLatestResponse> result = new ArrayList<>();
        for (BeautyConsultationOrderUserConsultantLatest obj : bcoUserConsultantLatests) {
            BcoUserConsultantLatestResponse res = new BcoUserConsultantLatestResponse();
            try {
                BeanUtilEx.copyProperties(res, obj);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            result.add(res);
        }
        return result;
    }
}
